# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=nsync
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.22.0
pkgrel=1
pkgdesc="C library that exports various synchronization primitives (mingw-w64)"
arch=('any')
url="https://github.com/google/nsync"
license=('Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "make"
             "${MINGW_PACKAGE_PREFIX}-gcc")
_archive=${_realname}-${pkgver}
source=(${_archive}.tar.gz::https://github.com/google/nsync/archive/${pkgver}.tar.gz
        0001-nsync-mingw-w64.patch)
sha256sums=('caf32e6b3d478b78cff6c2ba009c3400f8251f646804bcb65465666a9cea93c4'
            '037c0c43c0298582e6ff9751bb15f35e75460626a91ce7565879e27b9fb8fc48')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  rm -rf "${srcdir}/${_archive}.orig"
  cp -rp "${srcdir}/${_archive}" "${srcdir}/${_archive}.orig"
  cd "${srcdir}/${_archive}"
  apply_patch_with_msg \
    0001-nsync-mingw-w64.patch
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DNSYNC_ENABLE_TESTS=OFF \
    -DBUILD_SHARED_LIBS=ON \
    ../${_archive}

  make -j1
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  #make test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/${_archive}"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
